/*背景图片自动切换*/
var bgSize = 6;
var index = 0;
for(var i = 0; i < bgSize; i++){
	$("#_back").append("<div class='back'></div>").find(".back").eq(i).css({"background-image" : "url('/EstateManager/Content/img/img_Login/0" + (i+1) + ".jpg')" , "display" : "none"});;
}
$("#_back").eq(1).css({"display" : "block"});
var bgTimer = setInterval(function(){
	$("#_back .back").eq(index).fadeOut("slow",function(){
		index++;
		if(index > bgSize){
			index = 0;
		}
		$(".back").eq(index).fadeIn("slow");
	});	
},5000);


/*数据出错时css设置*/
var addError = function(jq_obj){
	jq_obj.addClass("error");
	jq_obj.focus();
}

var removeError = function(jq_obj){
	jq_obj.removeClass("error");
}

/*光标移开时数据不为空检验*/
$("input.control").focusout(function(){
	if($(this).val() == "")
		addError($(this));
	else
		removeError($(this));
});
/*提交时数据校验*/
$("#submit").click(function(){
	if($('input[name=username]').val() == ""){
		addError($('input[name=username]'));
		
	}
	if($('input[name=password]').val() == ""){
		addError($('input[name=password]'));
		
	}
	
	$("#login_content .control").fadeOut("fast");
	$("#login_content").css({"height":"200px","width":"200px"});
	$("#head-icon-back").fadeIn("fast");
	$("#head-icon").fadeIn("fast");
	$.ajax({
		url:'user/check-login.do',
		method:"post",
		data:{"userName":$('input[name=username]').val(),"password":$('input[name=password]').val()},	
		success:function(data){
			
				setTimeout(function(){
					if(data.code == "1000"){
						localStorage.setItem("userName",data.data.username);
						localStorage.setItem("password",data.data.password);
						location.href='user/view-PersonalHome.do';
					}
					else{
				
						loginFail();
					}
				},1000);
			
		},
		error:function(){
			
			$("#error p").text("连接服务器失败");
			setTimeout(loginFail(),1000);
		}
		
	});
	var loginFail = function(){
				$("#head-icon-back").fadeOut("slow");
				$("#head-icon").fadeOut("slow");
				$("#login_content .control").fadeIn("fast");
				$("#login_content").css({"height":"400px","width":"300px"});
	}
});
